package com.example.administrator.commonlibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.example.administrator.commonlibrary.welcomepage.MainActivity;

import java.util.List;

/**
 * 作者：wang_hong_wei
 * 创建时间：2018/9/8
 * 描述：
 */
public class FragmentUtil {
    /**
      * @desc 判断本Activity中是否添加了Fragment
      * @param fragmentManager 参数：getSupportFragmentManager()
      */
    public static boolean getFragmentList(FragmentManager fragmentManager){
        if (fragmentManager==null)throw  new NullPointerException("fragmentManager 不能为空! ! !");
        List<Fragment> fragments = fragmentManager.getFragments();
        return !fragments.isEmpty();
    }

    public static void addFragmentToActivity(FragmentManager fragmentManager,
                                             Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
        transaction.commit();
    }


    public static <T> T checkNotNull(T obj) {
        if (obj == null) {
            throw new NullPointerException();
        }
        return obj;
    }
}
